<?php

namespace App\Library\Service;

use App\Library\Logic\AuctionCertificationLogic;
use App\Library\Logic\BoxCertificationLogic;
use App\Library\Logic\InviteCertificationLogic;
use App\Library\Requests\Invite\CertificationApplyRequest;
use Illuminate\Http\JsonResponse;

class InviteCertificationService extends BaseService
{
    public function logic(): InviteCertificationLogic
    {
        if (empty($this->logic)) $this->logic = new InviteCertificationLogic;
        return $this->logic;
    }

    /**
     *
     * @param CertificationApplyRequest $request
     * @return JsonResponse
     *
     */
    public function apply(CertificationApplyRequest $request): JsonResponse
    {
        $model = $this->logic()->findByWhere(['userId' => USERID]);
        if (!is_null($model) && in_array($model->status, [0, 1])) return fail(633);
        $this->logic()->insertForModel(array_merge($request->all(), [
            'userId' => USERID
        ]));
        return success();
    }

    /**
     * 盲盒-拍卖-分销资质详情
     * @return JsonResponse
     *
     */
    public function allDetail(): JsonResponse
    {
        $data['box'] = BoxCertificationLogic::query()->where('user_id', USERID)->orderByDesc('created_at')->first();
        $data['auction'] = AuctionCertificationLogic::query()->where('user_id', USERID)->orderByDesc('created_at')->first();
        $data['invite'] = InviteCertificationLogic::query()->where('user_id', USERID)->orderByDesc('created_at')->first();

        return success($data);
    }
}
